package com.lxj.controller;

import com.lxj.domain.Category;
import com.lxj.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lxj
 * @date 2021/8/1 11:19
 * @Description:
 */
@RestController
@Api(tags = "商品分类管理")
@RequestMapping("prod/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("table")
    @ApiOperation("查询商品分类")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public ResponseEntity<List<Category>> loadCategory() {
        List<Category> list = categoryService.list();
        return ResponseEntity.ok(list);
    }


    @GetMapping("listCategory")
    @ApiOperation("查询商品所有的父分类")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public ResponseEntity<List<Category>> listCategory() {
        List<Category> list = categoryService.loadParentCategory();
        return ResponseEntity.ok(list);
    }


    @PostMapping
    @ApiOperation("新增商品分类")
    @PreAuthorize("hasAuthority('prod:category:save')")
    public ResponseEntity<Void> saveCategory(@RequestBody Category category) {
        categoryService.save(category);
        return ResponseEntity.ok().build();
    }
}
